package cn.sciento.core;

import cn.sciento.core.exception.BaseExceptionHandler;
import cn.sciento.core.helper.DetailsExtractor;
import cn.sciento.core.net.RequestHeaderCopyInterceptor;
import cn.sciento.core.properties.CoreProperties;
import cn.sciento.core.properties.ServiceProperties;
import cn.sciento.core.user.OrganizationUserType;
import cn.sciento.core.user.PlatformUserType;
import cn.sciento.core.user.TenantUserType;
import cn.sciento.core.user.UserType;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@Configuration
@EnableConfigurationProperties({CoreProperties.class, ServiceProperties.class})
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
public class STongAutoConfiguration {
    @Bean
    public BaseExceptionHandler baseExceptionHandler() {
        return new BaseExceptionHandler();
    }

    @Bean
    public DetailsExtractor detailsExtractor(CoreProperties properties) {
        return new DetailsExtractor(properties);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setInterceptors(Collections.singletonList(new RequestHeaderCopyInterceptor()));
        return restTemplate;
    }

    @Bean
    public UserType platformUserType() {
        return (UserType) new PlatformUserType();
    }

    @Bean
    public UserType tenantUserType() {
        return (UserType) new TenantUserType();
    }

    @Bean
    public UserType organizationUserType() {
        return (UserType) new OrganizationUserType();
    }
}
